package com.poker.controller;

import com.poker.consts.Consts;
import com.poker.entity.Activity;
import com.poker.entity.ManagerInfo;
import com.poker.service.ActivityService;
import com.poker.util.Misc;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName ActivityController
 * @Description TODO
 * @Author duyuanyuan
 * @Date 2019/3/13 11:01
 * @Version 1.0
 **/
@Controller
@RequestMapping("/activity")
public class ActivityController extends BaseController{

    @Resource
    private ActivityService activityService;
    /*
     * 功能描述:分页查询活动列表
     * @param:  * @param model
     * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:49
     */
    @RequestMapping("/list")
    public String list(Model model,
                       @RequestParam(value = "page", defaultValue = "0") Integer page,
                       @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<Activity> data = activityService.findList(page,size,managerInfo);
        model.addAttribute("activity", new Activity());
        model.addAttribute("data", data);
        return managerPage("activity/list");
    }
    /*
     * 功能描述:分页加条件查询活动列表
     * @param:  * @param model
     * @param activity
     * @param page
     * @param size
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:50
     */
    @RequestMapping("/findQuery")
    public String findQuery(Model model,Activity activity,
                              @RequestParam(value = "page", defaultValue = "0") Integer page,
                              @RequestParam(value = "size", defaultValue = "50") Integer size){
        Integer managerInfo = getManagerInfo();
        Page<Activity> data = activityService.findListAll(page,size,activity,managerInfo);
        model.addAttribute("activity",activity);
        model.addAttribute("data", data);
        return managerPage("activity/list");

    }
     /*
      * 功能描述:查询活动信息返回修改页面
      * @param:  * @param model
      * @param id
      * @param editType
      * @return: java.lang.String
      * @auther: duyuanyuan
      * @date: 2019/3/18 10:50
      */
    @RequestMapping("/edit")
    public String edit(Model model, String id, String editType) {
        Activity activity = new Activity();
        if (Consts.EditType.EDIT.equals(editType)) activity = activityService.getById(id);
        ManagerInfo managerInfo = loginManager();
        Integer i;
        if(managerInfo.getClassify()!=null){
            i=managerInfo.getClassify();
        }else {
            i=null;
        }
        model.addAttribute("user",i);
        model.addAttribute("activity", activity);
        model.addAttribute("editType", editType);
        return managerPage("activity/edit");
    }
    /*
     * 功能描述:提交修改活动信息
     * @param:  * @param activity
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:51
     */
    @ResponseBody
    @RequestMapping("/doEdit")
    public String doEdit(HttpServletRequest request,Activity activity) {
        if (Misc.isEmpty(activity) || Misc.isEmpty(activity.getId())) toJson(false, "编辑信息为空", null);
        ManagerInfo managerInfo = loginManager();
        if(managerInfo.getClassify()!=null){
            activity.setClassify(managerInfo.getClassify());
        }
        activityService.update(activity);
        return toJson(true, "success", null);
    }
    /*
     * 功能描述:新增活动信息
     * @param:  * @param activity
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:51
     */
    @ResponseBody
    @RequestMapping("/add")
    public String add(Activity activity) {
        if (Misc.isEmpty(activity)) return toJson(false, "添加信息为空", null);
        ManagerInfo managerInfo = loginManager();
        if(managerInfo.getClassify()!=null){
            activity.setClassify(managerInfo.getClassify());
        }
        activityService.saveActivityAndContent(activity);
        return toJson(true, "success", null);
    }
    /*
     * 功能描述:删除活动信息
     * @param:  * @param id
     * @param page
     * @return: java.lang.String
     * @auther: duyuanyuan
     * @date: 2019/3/18 10:51
     */
    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        if (Misc.isEmpty(id)) return toJson(false, "id信息为空", null);

        if (Misc.isEmpty(page)) return toJson(false, "page信息为空", null);

        Activity activity = activityService.getById(id);
        if (Misc.isEmpty(activity)) return toJson(false, "目标信息不存在", null);

        activityService.deleteActivityAndContent(activity);

        return redirect("/activity/list");
    }
}
